/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       WebSSHController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/3      feiwe                         add
 */


package com.fengwei.controller;

import cn.hutool.core.util.StrUtil;
import com.fengwei.config.CommonProperty;
import com.fengwei.form.MdmsUser;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.util.PltResult;
import com.fengwei.util.VerifyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class WebSSHController {
    private final static Logger logger = LoggerFactory.getLogger(MdmsSshserverController.class);

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @Autowired
    private CommonProperty commonProperty;

    @GetMapping(value = "/webSSH/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        String id = request.getParameter("id");
        logger.debug("id:" + id);
        HttpSession session = request.getSession();
        String serverUserKey = (String) session.getAttribute("serverUserKey" + mdmsUser.getId());
        if (StrUtil.hasEmpty(serverUserKey)) {
            logger.error("未找到对应的用户key");
            throw new VerifyException(PltResult.RESULT_CF315);
        }

        commonProperty.getSshServerUserKeyMap().put("key" + id, serverUserKey);
        commonProperty.getSshServerUserKeyMap().put("user" + id, mdmsUser);

        //TODO:这个serverId要用随机数
        model.addAttribute("serverId", id);
        return "webssh";
    }
}
